package com.hoshiibuy.company.service.impl;

import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.company.mapper.IComOrganizationMapper;
import com.hoshiibuy.company.entity.ComOrganization;
import com.hoshiibuy.company.service.IComOrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构表 服务实现类
 * </p>
 *
 * @author WGJ
 * @since 2019-04-22
 */
@Service
public class ComOrganizationServiceImpl extends ServiceImpl<IComOrganizationMapper, ComOrganization> implements IComOrganizationService {


    /**
     * @param
     * @return com.hoshiibuy.
     * @description: 根据id查询机构信息
     */
    @Cacheable(cacheNames = "organization", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public ComOrganization getById(Long id) {
        ComOrganization comOrganization = baseMapper.selectById(id);
        System.out.println("执行了数据库操作");
        return comOrganization;
    }


    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "organization", allEntries = true)
    @Override
    public boolean updateById(ComOrganization organization) {
        organization.setModifiedtime(LocalDateTime.now());
        int row = baseMapper.updateById(organization);

        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改机构失败"));
        }
        return true;
    }


}
